import Vue from 'vue'
// import { Toast } from "vant";
import VueRouter from 'vue-router'
import store from '@/store'

import libs from '@/libs'

Vue.use(VueRouter)

const routes = [
  {
    // 首页
    path: '/',
    name: 'home',
    component: () => import("@/views/home/"),
  },
  {
    // 通知页面
    path: '/notice',
    name: 'notice',
    component: () => import("@/views/notice/"),
  },
  {
    // 桥梁信息
    path: '/bridge',
    name: 'bridge',
    component: () => import("@/views/bridge/"),
  },
  {
    // 预制梁信息
    path: '/prebridge',
    name: 'prebridge',
    component: () => import("@/views/prebridge/"),
  },
  {
    // 预制梁生产详情
    path: '/prebridge/:id',
    name: 'prebridgeDetail',
    component: () => import("@/views/prebridge/detail.vue"),
  },
  {
    // 预制梁二维码
    path: '/prebridge/qr/:id',
    name: 'prebridgeQr',
    component: () => import("@/views/prebridge/qr.vue"),
  },
  {
    // 计划管理
    path: '/plan',
    name: 'plan',
    component: () => import("@/views/plan/"),
  },
  {
    // 新增计划
    path: '/plan/append',
    name: 'planAppend',
    component: () => import("@/views/plan/append.vue"),
  },
  {
    // 修改计划
    path: '/plan/:id/:type',
    name: 'planEdit',
    component: () => import("@/views/plan/edit.vue"),
  },
  {
    // 日计划管理
    path: '/dayplan',
    name: 'dayplan',
    meta: { keep: true },
    component: () => import("@/views/dayplan"),
  },
  {
    // 日计划管理
    path: '/dayplan/set',
    name: 'dayplanSet',
    component: () => import("@/views/dayplan/set.vue"),
  },
  {
    // 周计划管理
    path: '/weekplan',
    name: 'weekplan',
    component: () => import("@/views/weekplan"),
  },
  {
    // 新增计划
    path: '/weekplan/append',
    name: 'weekplanAppend',
    component: () => import("@/views/weekplan/append.vue"),
  },
  {
    // 修改计划
    path: '/weekplan/:id',
    name: 'weekplanEdit',
    component: () => import("@/views/weekplan/edit.vue"),
  },
  {
    // 生产汇总
    path: '/produce',
    name: 'produce',
    component: () => import("@/views/produce"),
  },
  {
    // 生产汇总
    path: '/produce/summary/:type',
    name: 'produceSummary',
    component: () => import("@/views/produce/summary"),
  },
  {
    // 梁片生产列表
    path: '/produce/summary/beam/:type',
    name: 'produceSummaryBeam',
    component: () => import("@/views/produce/summary/beam.vue"),
  },
  {
    // 钢筋加工记录
    path: '/steel/product',
    name: 'steelProduct',
    component: () => import("@/views/steel/product"),
  },
  {
    // 添加钢筋加工记录
    path: '/steel/product/append',
    name: 'steelProductAppend',
    component: () => import("@/views/steel/product/append.vue"),
  },
  {
    // 添加钢筋加工记录
    path: '/task/:name',
    name: 'task',
    component: () => import("@/views/task"),
  },
  {
    // 添加钢筋加工记录
    path: '/task/set/:id',
    name: 'taskSet',
    component: () => import("@/views/task/set.vue"),
  },
  {
    // 存梁管理
    path: '/storage',
    name: 'storage',
    component: () => import("@/views/storage"),
  },
  {
    // 存梁管理
    path: '/storage/edit',
    name: 'storageEdit',
    component: () => import("@/views/storage/edit.vue"),
  },
  {
    // 问题列表
    path: '/question',
    name: 'question',
    meta: { keep: true },
    component: () => import("@/views/question"),
  },
  {
    // 添加问题
    path: '/question/append',
    name: 'questionAppend',
    component: () => import("@/views/question/append.vue"),
  },
  {
    // 问题详情
    path: '/question/details',
    name: 'questionDetails',
    meta: { keep: true },
    component: () => import("@/views/question/details.vue"),
  },
  {
    // 修改问题
    path: '/question/edit',
    name: 'questionEdit',
    component: () => import("@/views/question/edit.vue"),
  },
  {
    // 待我处理
    path: '/handle',
    name: 'handle',
    meta: { keep: true },
    component: () => import("@/views/handle"),
  },
  {
    // 问题详情
    path: '/handle/details',
    name: 'handleDetails',
    meta: { keep: true },
    component: () => import("@/views/handle/details.vue"),
  },
  {
    // 视频监控
    path: '/monitor',
    name: 'monitor',
    component: () => import("@/views/monitor"),
  },
  {
    // 小车移动轨迹
    path: '/cartrack',
    name: 'cartrack',
    component: () => import("@/views/cartrack"),
  },
  {
    // 添加小车轨迹
    path: '/cartrack/append',
    name: 'cartrack/append',
    component: () => import("@/views/cartrack/append"),
  },
  {
    // 小车当前位置
    path: '/carposition',
    name: 'carposition',
    component: () => import("@/views/carposition"),
  },
  {
    // 扫码页面
    path: '/scan',
    name: 'scan',
    component: () => import("@/views/scan"),
  },
  {
    // 视频播放
    path: '/player',
    name: 'player',
    component: () => import("@/views/player"),
  },
  {
    // 视频播放
    path: '/player/video',
    name: 'playerVideo',
    component: () => import("@/views/player/video.vue"),
  },
  {
    // 我的
    path: '/my',
    name: 'my',
    component: () => import("@/views/my"),
  },
  {
    // 修改个人信息
    path: '/my/edit',
    name: 'myEdit',
    component: () => import("@/views/my/edit"),
  },
  {
    // 修改密码
    path: '/my/pwd',
    name: 'myPwd',
    component: () => import("@/views/my/pwd"),
  },
  {
    // 修改密码
    path: '/my/about',
    name: 'myAbout',
    component: () => import("@/views/my/about"),
  },
  {
    // 修改密码
    path: '/my/share',
    name: 'myShare',
    component: () => import("@/views/my/share"),
  },
  {
    path: '/download',
    name: 'download',
    meta: { noAuth: true },
    component: () => import("@/views/download/"),
  },
  {
    path: '/login',
    name: 'login',
    meta: { noAuth: true },
    component: () => import("@/views/login/"),
  },
  {
    path: '/login/protocol',
    name: 'loginProtocol',
    meta: { noAuth: true },
    component: () => import("@/views/login/protocol.vue"),
  },
  {
    path: '/login/forget',
    name: 'loginForget',
    meta: { noAuth: true },
    component: () => import("@/views/login/forget.vue"),
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach(async (to, from, next) => {
  if (!to.meta.noAuth) {
    const token = libs.store.getItem('token')
    if (!token) {
      return next({ name: 'login' })
    }

    // 检查用户信息
    const userInfo = store.state.user.userInfo
    if (!userInfo) {
      // 初始化用户信息
      await store.dispatch('user/init')
      // 获取预先加载数据
      store.dispatch('pre/init')
      next()
    } else {
      next()
    }

    
  } else {
    next()
  }
})

// eslint-disable-next-line no-unused-vars
router.afterEach((to) => {

})

export default router
